// import {
// 	baseUrl
// } from './env'
const axios = require('axios');


import { getCookie, getQueryString } from './util'

export default async (type = 'GET', url = '', data = {}, method = 'fetch') => {

	//把空属性去掉。
	Object.keys(data).forEach(key => {
		if(data[key] === undefined){
			delete data[key];
		}
	})

	const company_code = getQueryString('company_code');

	if(company_code === undefined || company_code == null){
		data['company_code'] = 'LW00035';
	}else{
		data['company_code'] = company_code;
	}

	type = type.toUpperCase();

	if (type == 'GET') {
		let dataStr = ''; //数据拼接字符串
		Object.keys(data).forEach(key => {
			dataStr += key + '=' + data[key] + '&';
		})

		if (dataStr !== '') {
			dataStr = dataStr.substr(0, dataStr.lastIndexOf('&'));
			url = url + '?' + dataStr;
		}
	}

	if (window.fetch && method == 'fetch') {

		let requestConfig = {
			credentials: 'include',
			method: type,
			headers: {
				'Accept': 'application/json',
				'Content-Type': 'application/json',
			},
			mode: "cors",
			cache: "no-cache"
		}

		if (type == 'POST') {
			Object.defineProperty(requestConfig, 'body', {
				value: JSON.stringify(data)
			})
		}
		
		var responseJson = {};
		try {
			var response = await fetch(url, requestConfig);
			responseJson = await response.json();
			if(responseJson.errcode && responseJson.errcode != 0){
				console.log(url);
				console.log(responseJson.errmsg);
			}
		} catch (error) {
			//throw new Error(error)
			console.log(error);
		}
		return responseJson;
	} else {
		console.log('不是fetch请求');

		const config = {
			'method' : type,
			'url' : url,
			'data' : data,
			'headers' : {
				'Accept': 'application/json',
				'Content-Type': 'application/json',
			},
		};

		var response = {};
		try {
		    response = await axios(config);
		} catch (error) {
			console.log('axios 中的异常 ： ' + error);
			console.log(error);
		}

		return response.data;

		// let requestObj;
		// if (window.XMLHttpRequest) {
		// 	console.log('XMLHttpRequest开始');
		// 	requestObj = new XMLHttpRequest();
		// } else {
		// 	console.log('ActiveXObject开始');
		// 	requestObj = new ActiveXObject;
		// }

		// console.log('data');
		// //console.log(data);

		// let sendData = '';
		// if (type == 'POST') {
		// 	sendData = JSON.stringify(data);
		// }

		// console.log('sendData');
		// //console.log(sendData);
		// //console.log(url);

		// requestObj.open(type, url, true);
		// console.log('open');
		// requestObj.setRequestHeader("Accept", "application/json");
		// requestObj.setRequestHeader("Content-type", "application/json");
		// console.log('setRequestHeader');

		// try{
		// 	requestObj.send(sendData);
		// }catch(err){
		// 	console.log('send异常');
		// 	console.log(err);
		// }
		// console.log('send');

		// requestObj.onerror = (e) => { 

		// 	console.log('onerror');
		// 	console.log(e);
		// };

		// requestObj.onreadystatechange = () => {
		// 	console.log('onreadystatechange 返回');
		// 	console.log(requestObj.readyState);
		// 	console.log(requestObj.status);
		// 	if (requestObj.readyState == 4) {
		// 		if (requestObj.status == 200) {
		// 			let obj = requestObj.response;
		// 			console.log('requestObj.readyState == 4 && requestObj.status == 200');
		// 			//console.log(obj);
		// 			if (typeof obj !== 'object') {
		// 				obj = JSON.parse(obj);
		// 			}
		// 			console.log('返回obj');
		// 			return obj
		// 		} else {
		// 			console.log('2222222');
		// 			throw new Error(requestObj)
		// 		}
		// 	}
		// }
	}
}